package singlelist;

import listinterface.IList;
import myexception.ListIndexOutOfBoundsException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2025-01-16
 * Time: 10:40
 */
public class Test {
    public static MySingList.ListNode mergeTwoLists(MySingList.ListNode headA,
                                                    MySingList.ListNode headB) {
        //定义一个傀儡节点（虚拟节点）：头节点 此时涉及到了获取静态内部类
        MySingList.ListNode newH = new MySingList.ListNode(-1);
        MySingList.ListNode tmp = newH;
        while (headA != null && headB != null) {
            if (headA.val > headB.val) {
                tmp.next = headB;
                headB = headB.next;
                //tmp = tmp.next;

            } else {
                tmp.next = headA;
                headA = headA.next;
                //tmp = tmp.next;
            }
            tmp = tmp.next;
        }
        if (headA != null) {
            tmp.next = headA;
        }
        if (headB != null) {
            tmp.next = headB;
        }

        return newH.next;
    }

    public static MySingList.ListNode getIntersectionNode(MySingList.ListNode headA,
                                                          MySingList.ListNode headB) {
        //1.假定A链表长，B链表短
        MySingList.ListNode pl = headA;
        MySingList.ListNode ps = headB;

        //2.分别求两个链表的长度
        int len1 = 0;
        int len2 = 0;
        while (pl != null) {
            len1++;
            pl = pl.next;
        }
        while (ps != null) {
            len2++;
            ps = ps.next;
        }
        pl = headA;
        ps = headB;

        //3.求长度的差值
        int len = len1 - len2;
        //len<0 --> pl = headB  ps = headA   len = len2 - len1
        if (len < 0) {
            pl = headB;
            ps = headA;
            len = len2 - len1;
        }
        //4.确定pl指向的节点一定是长链表 ps指向的节点一定是短链表

        //5.让pl走len步
        while (len != 0) {
            pl = pl.next;
            len--;
        }
        //6.ps和pl同时走 直到相遇
        while (pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        if (pl == null) {
            return null;
        }
        return pl;
    }

    public static void createCut(MySingList.ListNode headA,
                                 MySingList.ListNode headB){
        headB.next.next = headA.next.next.next;
    }

    public static void main(String[] args) {
        MySingList mySingList1 = new MySingList();
        mySingList1.addLast(12);
        mySingList1.addLast(23);
        mySingList1.addLast(34);
        mySingList1.addLast(45);
        mySingList1.addLast(56);
        mySingList1.addLast(67);
        mySingList1.addLast(12);

        mySingList1.display();
        System.out.println("===============");

        mySingList1.createLoop();

         boolean hasCycle =mySingList1.hasCycle();
        System.out.println(hasCycle);
    }
    public static void main8(String[] args) {
        MySingList mySingList1 = new MySingList();
        mySingList1.addLast(12);
        mySingList1.addLast(23);
        mySingList1.addLast(34);
        mySingList1.addLast(45);
        mySingList1.addLast(56);
        mySingList1.addLast(67);
        mySingList1.display();
        System.out.println("===============");

        MySingList mySingList2 = new MySingList();
        mySingList2.addLast(13);
        mySingList2.addLast(25);
        mySingList2.addLast(35);
        mySingList2.addLast(146);
        mySingList2.addLast(156);
        mySingList2.display();
        System.out.println("==========测试======");

        createCut(mySingList1.head,mySingList2.head);
        mySingList2.display();

        MySingList.ListNode ret = getIntersectionNode(mySingList1.head, mySingList2.head);
        System.out.println(ret.val);


    }

    public static void main7(String[] args) {
        MySingList mySingList = new MySingList();

        mySingList.addLast(12);
        mySingList.addLast(23);
        mySingList.addLast(34);
        mySingList.addLast(45);
        mySingList.addLast(56);
        mySingList.addLast(67);
        mySingList.addLast(78);
        mySingList.addLast(89);
        mySingList.display();

        int node = mySingList.kthToLast(10);
        System.out.println(node);
    }

    public static void main6(String[] args) {
        MySingList mySingList = new MySingList();

        mySingList.addLast(12);
        mySingList.addLast(23);
        mySingList.addLast(34);
        mySingList.addLast(45);
        mySingList.addLast(56);
        mySingList.addLast(67);
        mySingList.display();

        MySingList.ListNode node = mySingList.middleNode1();

        if (node == null) {
            System.out.println("链表为空");
        } else {
            System.out.println(node.getVal());
        }
    }

    public static void main5(String[] args) {
        MySingList mySingList = new MySingList();

        mySingList.addFirst(12);
        mySingList.addFirst(23);
        mySingList.addFirst(34);
        mySingList.addFirst(45);
        mySingList.addFirst(56);
        mySingList.addFirst(67);
        mySingList.display();

        MySingList.ListNode node = mySingList.middleNode();
        if (node == null) {
            System.out.println("链表为空");
        } else {
            System.out.println(node.getVal());

        }
    }

    public static void main4(String[] args) {
        IList mySingList = new MySingList();

        mySingList.addFirst(12);
        mySingList.addFirst(23);
        mySingList.addFirst(34);
        mySingList.addFirst(45);
        mySingList.addFirst(56);
        mySingList.display();

        MySingList.ListNode node = ((MySingList) mySingList).reverseList();
        //mySingList.display(node);
    }

    public static void main3(String[] args) {
        IList mySingList = new MySingList();
        mySingList.addFirst(23);
        mySingList.addFirst(23);
        mySingList.addFirst(23);
        mySingList.addFirst(23);
        mySingList.addFirst(23);

        mySingList.display();

        mySingList.removeAllKey(23);
        mySingList.display();
    }

    public static void main2(String[] args) throws ListIndexOutOfBoundsException {
        IList mySingList = new MySingList();
        mySingList.addFirst(12);
        mySingList.addFirst(23);
        mySingList.addFirst(34);
        mySingList.addFirst(45);
        mySingList.addFirst(56);

        mySingList.display();

        mySingList.addLast(30);
        mySingList.display();

        try {
            mySingList.addIndex(5, 5);
        } catch (ListIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        mySingList.remove(45);

        mySingList.display();
    }

    public static void main1(String[] args) {
        MySingList mySingList = new MySingList();
        mySingList.creatList();
        System.out.println("-------");
        mySingList.display();

        System.out.println(mySingList.size());

        System.out.println(mySingList.contains(23));
    }
}
